package com.tensquare.friend.controller;

import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/friend")
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private HttpServletRequest request;
    /*
    * PUT/friend/like/{friendid}/{type}添加好友或非好友*
    type:1喜欢 2：不喜欢
    /
     */
    @PutMapping("/like/{friendid}/{type}")
    public Result friend(@PathVariable String friendid,@PathVariable String type){
        Claims claims_user = (Claims) request.getAttribute("claims_user");
        if(claims_user==null){
            return  new Result(false, StatusCode.ERROR,"权限不足");
        }
        String userId = claims_user.getId();
        if(type!=null){
            if("1".equals(type)){
                //userid喜欢friendid
                int i = friendService.friend(userId, friendid);
                if(i==0){
                    return new Result(false,StatusCode.ERROR,"你已经喜欢过对方了！");
                }
                if(i==1){
                    return new Result(true,StatusCode.OK,"对不起擅自喜欢上了你！");
                }
            }
            if("2".equals(type)){
                //userid不喜欢friendid
                int i = friendService.nofriend(userId, friendid);
                if(i==0){
                    return new Result(false,StatusCode.ERROR,"讨厌过了！");
                }
                if(i==1){
                    return new Result(true,StatusCode.OK,"讨厌讨厌讨厌讨厌讨厌！");
                }
            }
        }
        return  new Result(false,StatusCode.ERROR,"参数异常");
    }
    /*
    DELETE/friend/{friendid}删除好友
    * */

    @DeleteMapping("/{friendid}")
    public Result deleteFriend(@PathVariable String friendid){
        Claims claims_user = (Claims) request.getAttribute("claims_user");
        if(claims_user==null){
            return  new Result(false, StatusCode.ERROR,"权限不足");
        }
        String userid = claims_user.getId();
        friendService.deleteFriend(userid,friendid);
        return new Result(true,StatusCode.OK,"妥");
    }
}
